package com.loan.psychicloan.imageloder;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * Created by watilion on 2017/12/21 0021.
 */

public class ImageLoaderUtil {

    public static final int PIC_LARGE = 0;
    public static final int PIC_MEDIUM =1;
    public static final int PIC_SMALL = 2;

    public static final int LOAD_STRATEGY_NORMAL =0;
    public static final int LOAD_STRATEGY_ONLY_WIFI = 1;

    public static boolean wifiFlag = false;

    private BaseImageLoaderStrategy mStrategy;

    public ImageLoaderUtil(){
        mStrategy = new PicassoImageLoaderStrategy();
    }

    //    private static ImageLoaderUtil mInstance;

//single instance
//    public static ImageLoaderUtil getInstance(){
//        if(mInstance ==null){
//            synchronized (ImageLoaderUtil.class){
//                if(mInstance == null){
//                    mInstance = new ImageLoaderUtil();
//                    return mInstance;
//                }
//            }
//        }
//        return mInstance;
//    }

    public void loadImage(Context context,ImageLoader imageLoader){
        mStrategy.loadImage(context,imageLoader);
    }

    public void setLoadImageStrategy(BaseImageLoaderStrategy strategy){
        mStrategy = strategy;
    }

    /**
     * 没有网络
     */
    public static final int NETWORKTYPE_INVALID = 0;
    /**
     * wap网络
     */
    public static final int NETWORKTYPE_WAP = 1;
    /**
     * 2G网络
     */
    public static final int NETWORKTYPE_2G = 2;
    /**
     * 3G和3G以上网络
     */
    public static final int NETWORKTYPE_3G = 3;
    /**
     * wifi网络
     */
    public static final int NETWORKTYPE_WIFI = 4;

    public static int getNetWorkType(Context context){

        int mNetWorkType = NETWORKTYPE_INVALID;
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(
                context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()){
            String type = networkInfo.getTypeName();
            if (type.equalsIgnoreCase("WIFI")){
                mNetWorkType = NETWORKTYPE_WIFI;
            }else if (type.equalsIgnoreCase("MOBILE")){
                String proxyHost = android.net.Proxy.getDefaultHost();
                mNetWorkType = TextUtils.isEmpty(proxyHost)
                        ?(isFastMobileNetWork(context)? NETWORKTYPE_3G : NETWORKTYPE_2G)
                        : NETWORKTYPE_WAP;
            }
        }else{
            mNetWorkType = NETWORKTYPE_INVALID;
        }
        return mNetWorkType;
    }

    private static boolean isFastMobileNetWork(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager)
                context.getSystemService(context.TELEPHONY_SERVICE);
        switch (telephonyManager.getNetworkType()){
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return false; // ~50-100 kbps
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return false;//~ 14-64 kbps
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return false;//~50-100kbps
            case  TelephonyManager.NETWORK_TYPE_EVDO_0:
                return false;//~400-1000kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return false;//~600-1400kbps
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return false;//~100kbps
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                return true;//~1-23Mbps
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return true;//~400-7000kbps
            case TelephonyManager.NETWORK_TYPE_EHRPD:
                return true;//~1-2Mbps
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
                return true;//~5Mbps
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return true;//~10-20Mbps
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return false;//~25kbps
            case TelephonyManager.NETWORK_TYPE_LTE:
                return true;//~10+Mbps
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                return false;//
            default:
                return false;
        }

    }
}
